package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检查组服务
 */
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private MenuService menuService;

    //分页查询
    public PageResult findPage(QueryPageBean pageBean) {
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();
        //使用分页助手插件实现分页查询
        PageHelper.startPage(currentPage,pageSize);
        Page<Role> page = roleDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //添加角色
    public void add(Integer[] permissionIds, Integer[] menuIds, Role role) {
        roleDao.add(role);
        Integer roleId = role.getId();
        this.setRoleAndPermission(roleId,permissionIds);
        this.setRoleAndMenu(roleId,menuIds);
    }

    //删除角色
    public void delete(Integer id) {
        roleDao.deleteRMByRoleId(id);
        roleDao.deleteRPByRoleId(id);
        roleDao.delete(id);
    }

    public void setRoleAndPermission(Integer roleId,Integer[] permissionIds) {
        //设置多对多关系
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("permissionId", permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }


    public void setRoleAndMenu(Integer roleId,Integer[] menuIds) {
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("menuId", menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }

    public Role findById(Integer id) {
        Role role = roleDao.findById(id);
        return role;
    }

    @Override
    public List<Integer>  findPermissionIdByRoleId(Integer id) {
        List<Integer> list = new ArrayList<>();
        list = roleDao.findPermissionIdByRoleId(id);
        return list;
    }

    @Override
    public List<Integer> findMenuIdByRoleId(Integer id) {
        List<Integer> list = new ArrayList<>();
         list = roleDao.findMenuIdByRoleId(id);
        return list;
    }

    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        Integer roleId = role.getId();
        roleDao.deleteRMByRoleId(roleId);
        roleDao.deleteRPByRoleId(roleId);
        roleDao.edit(role);
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                roleDao.addRPByRoleId(roleId,permissionId);
            }
        }
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                roleDao.addRMByRoleId(roleId,menuId);
            }
        }
    }


}
